package io.github.talelin.latticy.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.dto.DocumentDTO;
import io.github.talelin.latticy.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.model.DocumentDO;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;

/**
 * 文档资料表前端控制器
 *
* @author generator@TaleLin
* @since 2020-12-29
*/
@RestController
@RequestMapping("/cms/document")
@PermissionModule(value = "文档资料管理")
public class IDocumentController {

    @Autowired
    private DocumentService documentService;

    @PostMapping("")
    @PermissionMeta(value = "添加文档资料")
    @GroupRequired
    public CreatedVO create(@RequestBody @Validated DocumentDTO dto) {
        documentService.create(dto);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    @PermissionMeta(value = "修改文档资料")
    @GroupRequired
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Long id,
                            @RequestBody @Validated DocumentDTO dto) {
        documentService.updated(id, dto);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    @PermissionMeta(value = "删除文档资料")
    @GroupRequired
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Long id) {
        documentService.delete(id);
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    public DocumentDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Long id) {
        return documentService.getDocument(id);
    }

    @GetMapping("/page")
    public PageResponseVO<DocumentDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Long count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Long page
    ) {
        Page<DocumentDO> pager = new Page<>(page, count);
        IPage<DocumentDO> paging = documentService.getBaseMapper().selectPage(pager, null);
        return new PageResponseVO<>(paging.getTotal(), paging.getRecords(), paging.getCurrent(), paging.getSize());
    }

}
